﻿using Microsoft.AspNetCore.Mvc.Controllers;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.OpenApi.Models;
 

namespace MS.Core.Extensions
{
    public static class SwaggerSetup
    {
        public static void AddSwaggerSetup(this IServiceCollection services)
        {
            services.AddSwaggerGen(options =>
            {
                options.SwaggerDoc("v1", new OpenApiInfo { Title = "护理API文档", Version = "v1" });
                options.CustomOperationIds(apiDesc =>
                   {
                       var controllerAction = apiDesc.ActionDescriptor as ControllerActionDescriptor;
                       return controllerAction.ControllerName + "-" + controllerAction.ActionName;
                   });
                var scheme = new OpenApiSecurityScheme()
                {
                    Description = "Authorization header.\r\n Example:'Bearer 123'",
                    Reference = new OpenApiReference { Type = ReferenceType.SecurityScheme, Id = "Authorization" },
                    Scheme = "oauth2",
                    Name = "Authorization",
                    In = ParameterLocation.Header,
                    Type = SecuritySchemeType.ApiKey
                };
                options.AddSecurityDefinition("Authorization", scheme);
                var requirement = new OpenApiSecurityRequirement();
                requirement[scheme] = new List<string>();
                options.AddSecurityRequirement(requirement);

                //为文档添加注释 AppContext.BaseDirectory
                options.IncludeXmlComments(Path.Combine(AppContext.BaseDirectory, "ReportDesignerServer.xml"), true);
            });
        }
    }
}
